﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mBook.Entity;
using JJY.CommonHelper;
using HtmlAgilityPack;
using System.Text;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class FetchData_AmazonWeekTopList : System.Web.UI.Page
{
    private string EmptyString = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtUrl.Text = "http://www.amazon.cn/gp/bestsellers/%E5%9B%BE%E4%B9%A6/658391051/ref=sv_b_3" + Environment.NewLine +
                            "http://www.amazon.cn/bestsellers/%E5%9B%BE%E4%B9%A6/658391051?page=2" + Environment.NewLine +
                            "http://www.amazon.cn/bestsellers/%E5%9B%BE%E4%B9%A6/658391051?page=3" + Environment.NewLine +
                            "http://www.amazon.cn/bestsellers/%E5%9B%BE%E4%B9%A6/658391051?page=4" + Environment.NewLine +
                            "http://www.amazon.cn/bestsellers/%E5%9B%BE%E4%B9%A6/658391051?page=5";

            txtUrl2.Text = "http://www.amazon.cn/bestsellers/%E5%B0%8F%E8%AF%B4/658393051" + Environment.NewLine +
                            "http://www.amazon.cn/bestsellers/%E5%B0%8F%E8%AF%B4/658393051?page=2" + Environment.NewLine +
                            "http://www.amazon.cn/bestsellers/%E5%B0%8F%E8%AF%B4/658393051?page=3" + Environment.NewLine +
                            "http://www.amazon.cn/bestsellers/%E5%B0%8F%E8%AF%B4/658393051?page=4" + Environment.NewLine +
                            "http://www.amazon.cn/bestsellers/%E5%B0%8F%E8%AF%B4/658393051?page=5";            
        }
    }

    protected void btnFetch_OnClick(object sender, EventArgs e)
    {
        try
        {
            string[] urlSet = txtUrl.Text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            FetchData(urlSet);            
        }
        catch (Exception ex)
        {
            lblInfo.Text = ex.ToString();
        }
    }


    protected void btnFetch2_OnClick(object sender, EventArgs e)
    {
        try
        {
            string[] urlSet = txtUrl2.Text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            FetchData(urlSet);
        }
        catch (Exception ex)
        {
            lblInfo.Text = ex.ToString();
        }
    }

    private void FetchData(string[] urlSet)
    {
        IList<AmazonTop> amazonTopSet = new List<AmazonTop>();

        foreach (string url in urlSet)
        {
            amazonTopSet = amazonTopSet.Concat(GetData(url)).ToList();
        }

        ExportTable(CreateTable(amazonTopSet));

        lblInfo.Text = "Fetch Successful!";
    }

    private HtmlTable CreateTable(IList<AmazonTop> amazonTopSet)
    {
        HtmlTable result = new HtmlTable();

        HtmlTableRow rowTitle = new HtmlTableRow();
        rowTitle.Cells.Add(new HtmlTableCell() { InnerText = "排名" });
        rowTitle.Cells.Add(new HtmlTableCell() { InnerText = "书名" });
        rowTitle.Cells.Add(new HtmlTableCell() { InnerText = "作者" });
        rowTitle.Cells.Add(new HtmlTableCell() { InnerText = "出版社" });
        rowTitle.Cells.Add(new HtmlTableCell() { InnerText = "出版时间" });
        result.Rows.Add(rowTitle);

        foreach (AmazonTop amazonTop in amazonTopSet)
        {
            HtmlTableRow row = new HtmlTableRow();
            row.Cells.Add(new HtmlTableCell() { InnerText = amazonTop.Ranking.ToString() });
            row.Cells.Add(new HtmlTableCell() { InnerText = amazonTop.BookName });
            row.Cells.Add(new HtmlTableCell() { InnerText = amazonTop.Author });
            row.Cells.Add(new HtmlTableCell() { InnerText = amazonTop.Press });
            row.Cells.Add(new HtmlTableCell() { InnerText = amazonTop.PressDate });
            result.Rows.Add(row);
        }
        return result;
    }

    private IList<AmazonTop> GetData(string url)
    {
        IList<AmazonTop> result = new List<AmazonTop>();

        string str = HttpRequestService.GetHtmlSourceByUrl(url,Encoding.UTF8)
                .Replace(@"\t", EmptyString).Replace(@"\r", EmptyString).Replace(@"\n", EmptyString)
                .Replace("&nbsp;", EmptyString).Replace("　", EmptyString).Trim();

        HtmlDocument doc = new HtmlDocument();
        doc.LoadHtml(str);

        HtmlNodeCollection nodeBookSet = doc.DocumentNode.SelectNodes("//div[@class='item']");

        int ranking = 0;
        string bookName = string.Empty;
        string author = string.Empty;
        string press = string.Empty;
        string pressDate = string.Empty;

        foreach (HtmlNode nodeDiv in nodeBookSet)
        {
            try
            {
                if (nodeDiv != null)
                {
                    HtmlNode nodeRanking = doc.DocumentNode.SelectNodes(nodeDiv.XPath + "/div[@class='number']").FirstOrDefault();
                    HtmlNode nodeBookName = doc.DocumentNode.SelectNodes(nodeDiv.XPath + "/div[@class='Name']").FirstOrDefault();

                    if (nodeRanking != null && nodeBookName != null)
                    {
                        ranking = int.Parse(nodeRanking.InnerText.Replace(".", string.Empty).Trim());
                        bookName = nodeBookName.InnerText.Replace("\t", string.Empty);
                        HtmlNode nodeUrl = nodeBookName.SelectNodes(nodeBookName.XPath + "/a").FirstOrDefault();
                        if (nodeUrl != null)
                        {
                            string urlDetails = HttpRequestService.GetHtmlSourceByUrl(nodeUrl.Attributes["href"].Value.ToString(), Encoding.UTF8)
                                                .Replace(@"\t", EmptyString).Replace(@"\r", EmptyString).Replace(@"\n", EmptyString)
                                                .Replace("&nbsp;", EmptyString).Replace("　", EmptyString).Trim();
                            HtmlDocument docDetails = new HtmlDocument();
                            docDetails.LoadHtml(urlDetails);

                            HtmlNode nodeAuthor = docDetails.DocumentNode.SelectNodes("//div[@class='buying']")[1];
                            HtmlNode nodePress = docDetails.DocumentNode.SelectNodes("//td[@class='bucket']/div")[0];

                            author = nodeAuthor.InnerText.Split('~')[1];
                            if (nodePress != null)
                            {
                                HtmlNode liPress = docDetails.DocumentNode.SelectNodes(nodePress.XPath + "/ul/li")[0];
                                press = liPress.InnerText.Trim().Split(';')[0].Trim();
                                pressDate = liPress.InnerText.Trim().Split(';')[1].Trim();
                            }
                        }
                    }

                    result.Add(new AmazonTop()
                    {
                        AmazonID = Guid.NewGuid(),
                        BookName = bookName,
                        Author = author,
                        Press = press,
                        PressDate = pressDate,
                        Ranking = ranking,
                        CreateDate = DateTime.Now,
                        ModifyDate = DateTime.Now,
                        IsDeleted = false
                    });
                }
            }
            catch (Exception ex)
            {
 
            }
        }

        return result;
    }

    private void ExportTable(HtmlTable table)
    {
        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(System.DateTime.Now.ToString("yyyy-MM-dd") + ".xls", System.Text.Encoding.UTF8));
        HttpContext.Current.Response.ContentType = "application/ms-xls";
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.Charset = "UTF-8";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;


        //Init HtmlWriter
        StringWriter writer = new StringWriter();
        HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);

        table.RenderControl(htmlWriter);

        HttpContext.Current.Response.Write(writer.ToString());
        HttpContext.Current.Response.End();
    }
}